/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditFeaturePresenter.java
 * Date: 09/02/2011
 */

package com.ingenius.sau.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListFeaturesEvent;
import com.ingenius.sau.client.service.GuidelinesServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.guidelines.Factor;
import com.ingenius.sau.shared.model.guidelines.Feature;

/**
 * Class <strong>EditFeaturePresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditFeaturePresenter implements EditPresenter {

	public interface EditFeatureDisplay extends Display {

		String getName();

		void setName(String name);

		String getDescription();

		void setDescription(String description);

		String getAspects2Evaluate();

		void setAspects2Evaluate(String text);

		void show();

		void hide();
	}

	private final HandlerManager eventBus;
	private final GuidelinesServiceAsync rpcService;
	private final EditFeatureDisplay display;
	private Feature feature;

	public EditFeaturePresenter(HandlerManager eventBus,
			GuidelinesServiceAsync rpcService, EditFeatureDisplay display,
			String featureId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		display.setPresenter(this);

		if (featureId == null) {
			feature = new Feature();

		} else {
			loadBean(featureId);
		}

	}

	private void loadBean(String featureId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getFeature(featureId, new AsyncCallback<Feature>() {
			@Override
			public void onSuccess(Feature result) {
				if (result == null) {
					onFailure(new Throwable("Database error"));
					return;
				}
				feature = result;
				display.setAspects2Evaluate(feature.getAspects2Evaluate());
				display.setDescription(feature.getDescription());
				display.setName(feature.getName());
				hideStatusMessage();
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_GET + Feature.class.getName(),
						caught);
				feature = new Feature();
				// TODO i18n
				String failedMessage = "Failed to retrieve feature";
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	@Override
	public void doSaveOrUpdate() {
		feature.setName(display.getName());
		feature.setDescription(display.getDescription());
		feature.setAspects2Evaluate(display.getAspects2Evaluate());
		String currentFactorId = (String) AppController
				.getAttribute(Constants.CURRENT_FACTOR_ID);
		feature.setFactor(new Factor(currentFactorId));

		// TODO i18n
		final String failedMessage = "Failed to save feature";
		final String successMessage = "Feature saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateFeature(feature, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					eventBus.fireEvent(new ListFeaturesEvent());
					display.showInfo(successMessage);
					hideStatusMessage();

				} else {
					onFailure(new Throwable("Database error"));
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_SAVE + Feature.class.getName(),
						caught);
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
